package org.neuedu.servlet;

import org.neuedu.bean.IndexLoader;
import org.neuedu.bean.User;
import org.neuedu.dao.ArticleDao;
import org.neuedu.dao.ArticleDaoImpl;
import org.neuedu.service.ArticleService;
import org.neuedu.service.ArticleServiceImpl;
import org.neuedu.service.UserService;
import org.neuedu.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ForwardIndexServlet",urlPatterns = "/index")
public class ForwardindexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User fly_user = (User) request.getSession().getAttribute("fly_user");
        Integer uid = fly_user.getId();
        UserService u =new UserServiceImpl();
        u.userhitcard(uid);
        request.setAttribute("sign","false");
        request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取 主要文章信息 的标识id
        String tid = request.getParameter("tid");
        if(tid == null){
            tid = "0";
        }
        // 加载首页信息
        // 1.文章分类；2.置顶列表；3.综合列表（不是固定）；4.回帖周榜；5.本周热议
        ArticleService service = new ArticleServiceImpl();
        IndexLoader indexLoader = service.loadIndexInfo(Integer.valueOf(tid));
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("fly_user");
        if(user != null){
            Integer id = user.getId();
            UserService u =new UserServiceImpl();
            boolean y=u.Hitcard(id);
            if(y==false){
                request.setAttribute("sign",y);
            }
            else{
                request.setAttribute("sign",y);
            }
        }
        request.getSession().setAttribute("indexInfo",indexLoader);
        request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request,response);
    }
}
